
from selenium.webdriver.support.ui import WebDriverWait
from datetime import datetime
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys

class Base():
    #封装浏览器方法
    def __init__(self,driver):
        self.driver=driver


    #封装打开首页的方法
    def open(self,url):
        self.driver.get(url)


    #封装定位元素方法
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            datatime=datetime.now().strftime('%Y%m%d %H%M%S')
            self.driver.save_screenshot(f'erro_{datatime}.png')
            print('页面元素未找到')



    #封装输入方法
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)


    #封装点击方法
    def click(self,loc):
        self.find_element(loc).click()


    #封装多窗口切换
    def window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])


    #封装滚动条操作
    def execute_script(self,loc):
        self.driver.execute_script(f'window.scrollTo(0,{loc})')

    #封装弹框方法1
    def select(self,loc,x):
        day1=Select(self.find_element(loc))
        day1.select_by_value(x)

    ##封装弹框方法2
    def select2(self,loc,x):
        day1=Select(self.find_element(loc))
        day1.select_by_index(x)

    ##封装弹框方法3
    def select3(self,loc,x):
        day1=Select(self.find_element(loc))
        day1.select_by_visible_text(x)


    #封装键盘事件
    def key(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)


